package com.sucisoft.base.application;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.sucisoft.base.Helper;
import com.taobao.sophix.PatchStatus;
import com.taobao.sophix.SophixManager;
import com.taobao.sophix.listener.PatchLoadStatusListener;

/**
 * 2033152950
 * Created by zf on 2018/3/27.
 */

public class AppSophix extends AppAbstract {

    private String rsaSecret() {
        return "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCgWcjZibzaW3dFBGzSWmMRRRSIu14b1oTcnPZUapE8pERgxsYRUuO1Wwb60/pZSkUkM3I1lUvDAPY7kHt4OuEXGQxD1MBfG2E+c+tPF9JPYOJyNooCh5VgietPWIbOk36b1vKfEjvE8QRhtmL0yh+nbBJc4fUYuSv6wExTdiykR0s4p26j1jSgcffVZTtaNKE6C6s5YGw3omlM3fLTx90Zf6ZN68LxuWHM/RCfsCgw6DRi9c96Vd3K2vIZpZpVrfr+5TcHfA/zC113HHlblQN0RFc7IVYrWf8PCZRW9JU8W5dR9W2As1kKBtvbzT8UYIqAzpb0oMZUqiAsZ9g2TZO7AgMBAAECggEALT4H8HDvawChn0SHW7aK+yVtjMKo49vb8s8KpXjH7789V7zdx6eGffXn+z9yBF1zMqqdT8cG8EdGMjoqzBp4xeaYUk8l0+/KuS9FtjQ3b5QbcDQ5LkuI9wsS7HlC1+g/scKtA/rGCGQkY1lthDm/QqK4b6KFX+tnIEG/h1DxeYv9gGB5NfyzOU5hClC3OdDn5TUL/5nPpAZB05EWclcYfbrTGIV75sQlw/GSqJXNRNfQV3eGx+mMWkYNpLW2D5PnMnTf72OCIBIIkV/70efUCr2HWIxSk1dA0skEzSfxtJm6aNNjAkk1QrL7FC67ju+a1RpDsuXZ0C+d5g9jWijfGQKBgQDXLW2xAGwjoZJg9vS8TU7hL+zHzXrLjnc3hHEgQN85sXykpBG/yhQJWz9FSNJTWt2BrBngg5m2hQIQyVCUiyDeZU+VvrjtL2RufJj7WPWbjWgBqOmT2hcyIV2tVmftiBk7RfpvFFQxNpLFXE8ZKM1MPR18wgS2IC706foXoxmcjQKBgQC+xZLn0P3sawvqNCB7I4uctOTuoGl+aV/VZogDWzDP+XHSJOTm8zOzrcRK++3S6qe8CqmCEfZzAU+ZNuwjqe8UO5tF7yBYHP0pmsxorWLyhQbrQfOZutQDkojsPy+Q7BorKvQdiV+CCdv7Cr58ySCrMpQdUwaoG24q8g6XwO2zZwKBgAF/R2wb3KTC0wnprfpBb3MRqkwZmekgrKOuQDtBOExmd7+NhLIY7drCz8Xjo9MAptnoIKX3RHKIqmMDg5QFA82IVu1rzk2YLtK1+zqEe/EBIuAgFL/6nH60zLoQB9kXQVWwPvXW5l8RSiQlaEd8iTEmm7Xqg63gu3LozdysPdlhAoGAYjJEyfpzmewH+AfY6EmFpaYfaQ09AAqxf6EFReT8nTDi+65t/MsWw9ggABDpe/W5XTuHwSIJ32y/UjhJGJpyK9H6vOa3mWNilLXPndoR4o/C3fnWsDzQwP3+aMylaU1+stWrTkDV3pPAndoL7yOAI18nimigNovYC1cJV+fA8uUCgYEAzf+m5U3Gox5Ryme0uOR9s99AT9xU1lx4qnzYyxMIRLjcyzwj/hkCzxpKRblPjmA0Qki4KghVo9MotRoFkx+zgpzBzl+0NMEyhUppQx63ojyWK4365kGKeuhI4PPwGUj8RmE9935Q9vkSYdz6+X4XUpAcINpUDRfeecTgwr3nBI4=\n";
    }

    private String appSecret() {
        return "29f78503adc6c086596d185e6cc35669";
    }

    private String idSercert() {
        return "24835691";
    }

    @Override
    public void attach() {
    }

    @Override
    public void init() {
        //阿里热修复
        initSophix(Helper.getContext(), Helper.getApplication());

    }

    /**
     * 返回当前程序版本名
     */
    public static String getAppVersionName(Context context) {
        String versionName = "";
        try {
            // ---get the package info---
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
            //versioncode = pi.versionCode;
            if (versionName == null || versionName.length() <= 0) {
                return "";
            }
        } catch (Exception e) {
            Log.e("VersionInfo", "Exception", e);
        }
        return versionName;
    }

    private void initSophix(final Context context, Application application) {
        SophixManager.getInstance().setContext(application)
                .setAppVersion(getAppVersionName(context))
                .setAesKey(null)
                .setSecretMetaData(idSercert(), appSecret(), rsaSecret())
                .setEnableDebug(true)
                .setPatchLoadStatusStub(new PatchLoadStatusListener() {
                    @Override
                    public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
                        // 补丁加载回调通知
                        if (code == PatchStatus.CODE_LOAD_SUCCESS) {
                            // 表明补丁加载成功
                            Log.i("success", "sophix");
                        } else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
                            // 表明新补丁生效需要重启. 开发者可提示用户或者强制重启;
                            // 建议: 用户可以监听进入后台事件, 然后应用自杀
                            restartApp(context);
                        } else if (code == PatchStatus.CODE_LOAD_FAIL) {
                            // 内部引擎异常, 推荐此时清空本地补丁, 防止失败补丁重复加载
                             SophixManager.getInstance().cleanPatches();
                        } else {
                            // 其它错误信息, 查看PatchStatus类说明
                        }
                    }
                }).initialize();

    }


    public void restartApp(Context context) {
        restartAPP(context, 2000);
    }

    private static void restartAPP(Context context, long Delayed) {

        android.os.Process.killProcess(android.os.Process.myPid());
    }

}
